package com.example.mq.mqsever.core;

import com.example.mq.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-08-20
 * Time: 17:57
 */
//表示一个存储消息的队列,此处的使用指的是消费者的使用
public class MSGQueue {
    //1.表示队列的身份标识
    private String name;
    //表示队列是否持久化
    private boolean durable;
    //为true表示队列.表示该队列只能被一个消费者占用
    //为FALSE.表示大家都可以使用
    private boolean exclusive = false;
    //表示是否被自动删除,如果该队列不再被使用为TRUE就被删除,否则保留,先不实现
    private boolean autoDelete = false;
    //指的是扩展参数,可实现可不实现,先不实现
    private Map<String, Object> arguments = new HashMap<>();
    //当前队列都有哪些消费者订阅了
    private List<ConsumerEnv> consumerEnvList=new ArrayList<>();
    //记录当前取到了第几个消费者,方便实现轮询
    private AtomicInteger consumerSeq=new AtomicInteger(0);
    //添加一个新的订阅者
    public void addConsumer(ConsumerEnv consumerEnv){

            consumerEnvList.add(consumerEnv);


    }
    //订阅者的删除暂时不考虑
    //挑选一个订阅者,用来处理当前的消息.
    //按照轮询的方法挑选
    public ConsumerEnv chooseConsumer(){
        if(consumerEnvList.size()==0){
            //该队列无人订阅
            return null;
        }
        //计算一下当前要取的元素的下标
        int index=consumerSeq.get()%consumerEnvList.size();
        consumerSeq.getAndIncrement();
        return consumerEnvList.get(index);
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }
//这俩用在数据库
   public String getArguments(){
       ObjectMapper objectMapper=new ObjectMapper();
       try {
           return objectMapper.writeValueAsString(arguments);
       } catch (JsonProcessingException e) {
           e.printStackTrace();
       }
       return "{}";
   }
public void setArguments(String argumentsJson){
        ObjectMapper objectMapper=new ObjectMapper();
    try {
      this.arguments=  objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String,Object>>() {
        });
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
}
//这俩在代码之间
    public Object getArgument(String key){
        return arguments.get(key);
    }

    public void setArguments(String key,Object value){
        arguments.put(key,value);
    }

    public void setArguments(Map<String ,Object> arguments){
        this.arguments=arguments;
    }
}
